<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- 缓动动画就是让元素运动速度有所变化，最常见的是让速度慢慢停下来
    思路：
    1.让盒子每次移动的距离慢慢变小，速度就会慢慢落下来。
    2.核心算法：（目标值-现在的位置）/10做为每次移动的距离步长
    3.停止的条件是：让当前盒子位置等于目标位置就停止定时器 -->
    <style>
      div {
        position: absolute;
        top: 100px;
        width: 100px;
        height: 100px;
        background-color: skyblue;
      }
    </style>
  </head>
  <body>
    <div></div>
    <button class="btn000">000</button>
    <button class="btn500">500</button>
    <button class="btn800">800</button>
    <script>
      var div = document.querySelector("div");
      var btn000 = document.querySelector(".btn000");
      var btn500 = document.querySelector(".btn500");
      var btn800 = document.querySelector(".btn800");
      function animate(obj, target) {
        clearInterval(obj.timer);
        obj.timer = setInterval(function () {
          //步长公式(目标值-现在位置)/10
          var step = (target - obj.offsetLeft) / 10;
          step = step > 0 ? Math.ceil(step) : Math.floor(step);
          if (obj.offsetLeft == target) {
            clearInterval(obj.timer);
          } else {
            obj.style.left = obj.offsetLeft + step + "px";
          }
        }, 15);
      }
      btn000.addEventListener("click", function () {
        animate(div, 0);
      });
      btn500.addEventListener("click", function () {
        animate(div, 500);
      });
      btn800.addEventListener("click", function () {
        animate(div, 800);
      });
    </script>
  </body>
</html>
